package edu.gtcfla.km.appmodules.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.common.web.json.ListResult;
import org.vsg.search.api.criterion.MapPaginaitionCriteria;
import org.vsg.search.api.criterion.PaginationCriteria;
import org.vsg.search.api.criterion.SearchCriteriaCls;
import org.vsg.security.annotation.RegisterRequestPermissionStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;

import edu.gtcfla.km.UploadFile;
import edu.gtcfla.km.appmodules.service.UploadFileManager;
import edu.gtcfla.km.appmodules.service.UploadFileService;
import edu.gtcfla.km.appmodules.vo.UploadFileListViewVO;

@Controller
@Lazy
public class DownloadFileController {


	private final String modulePath = "/knowledge";

	@Autowired(required = false)
	private UploadFileService uploadFileService;
	
	
	@Autowired(required = false)
	private UploadFileManager uploadFileManager;
	
	@RequestMapping(value = "/data/image")
	public void  accessFile(HttpServletResponse res ,  String fileName) throws 

Exception {

		File file = uploadFileManager.getRealPath(fileName);
		
		int lastLoc = fileName.lastIndexOf(".");
		
		String extFile = fileName.substring( lastLoc );
		
		String mimeType = null;
		if (".gif".contains(extFile)) {
			mimeType = "image/gif";
		}
		else if (".jpeg .jpg".contains( extFile )) {
			mimeType = "image/jpg";		
		}
		else if (".png".contains( extFile )) {
			mimeType = "image/png";		
		}
		
		res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		res.setHeader("Content-Type: ", mimeType);
		
		InputStream fileInputStream  = new FileInputStream(file);
		
		OutputStream os = res.getOutputStream();
		IOUtils.copyLarge( fileInputStream , os);
		
		os.flush();
		os.close();

	}

	@RequestMapping(value = "/data/file")
	public void  accessAttachmentFile(HttpServletResponse res ,  String docId ,  Short 

onlineView) throws Exception {
		
		// --- get current use id ---
		UploadFile upladedFile = uploadFileService.loadUploadFile(docId);
		String fileName = upladedFile.getFileName();
		
		short onlineview = 1;
		if (onlineView != null) {
			onlineview = new Short( onlineView );
		}
		
		if (onlineview == 0) {
			String mimeType = null;
			// --- upload file ---
			if (".doc .docx".contains( upladedFile.getExt() )) {
				mimeType = "application/msword";
				res.setHeader("Content-Disposition", 

"inline;fileName="+fileName);			
			}
			else if (".xls .xlsx".contains( upladedFile.getExt() )) {
				mimeType = "application/vnd.ms-excel";		
				res.setHeader("Content-Disposition", 

"inline;fileName="+fileName);	
			}		
			else if (".ppt".contains( upladedFile.getExt() )) {
				mimeType = "application/vnd.ms-powerpoint";
				res.setHeader("Content-Disposition", 

"inline;fileName="+fileName);				
			}		
			else if (".zip".contains( upladedFile.getExt() )) {
				mimeType = "application/zip";		
				res.setHeader("Content-Disposition", 

"attachment;fileName="+fileName);
			}
			
			
			else {
				mimeType = "application/octet-stream";
				res.setHeader("Content-Disposition", 

"attachment;fileName="+fileName);
			}

			res.setHeader("Content-Type: ", mimeType);
		}
		File file = uploadFileManager.getRealPath(fileName);
				
		InputStream fileInputStream  = new FileInputStream(file);
		
		OutputStream os = res.getOutputStream();
		IOUtils.copyLarge( fileInputStream , os);
		
		fileInputStream.close();
		os.flush();
		os.close();

	}	

	@RequestMapping(value = "/data/media")
	public void accessMediaFile(HttpServletResponse res ,String docId) throws Exception 

{
		// --- get current use id ---
		String mimeType = null;
		UploadFile upladedFile = uploadFileService.loadUploadFile(docId);
		String fileName = upladedFile.getFileName();
		// --- upload file ---
		if (".avi".contains( upladedFile.getExt().toLowerCase() )) {
			mimeType = "video/avi";		
			res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		}
		else if (".asf".contains( upladedFile.getExt().toLowerCase() )) {
			mimeType = "video/x-ms-asf";		
			res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		}
		else if (".mp4".contains( upladedFile.getExt().toLowerCase() )) {
			mimeType = "video/mpeg4";		
			res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		}			
		else if (".wmv".contains( upladedFile.getExt().toLowerCase() )) {
			mimeType = "video/x-ms-wmv";		
			res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		}			
		else if (".mpg".contains( upladedFile.getExt().toLowerCase() )) {
			mimeType = "video/mpeg";		
			res.setHeader("Content-Disposition", "inline;fileName="+fileName);
		}			
		
		else {
			mimeType = "application/octet-stream";
			res.setHeader("Content-Disposition", 

"attachment;fileName="+fileName);
		}

		res.setHeader("Content-Type: ", mimeType);

		File file = uploadFileManager.getRealPath(fileName);
				
		InputStream fileInputStream  = new FileInputStream(file);
		
		OutputStream os = res.getOutputStream();
		IOUtils.copyLarge( fileInputStream , os);
		os.flush();
		os.close();
		fileInputStream.close();
	}
	
	private ObjectMapper mapper = new ObjectMapper();

	@SearchCriteriaCls(criteriaClass = MapPaginaitionCriteria.class)
	@RequiresUser
	@RegisterRequestPermissionStrategy(value = 

edu.gtcfla.km.core.ApplicationConstant.DATA_RERESOURCE + modulePath	+ 

"/content/bigview.json", skipCheck = true)
	@RequestMapping(value = edu.gtcfla.km.core.ApplicationConstant.DATA_RERESOURCE + 

modulePath + "/content/bigview.json")
	public @ResponseBody
	ListResult showAttachmentBigView(PaginationCriteria criteria) throws Exception {


		//Set<UploadFileListViewVO> allList = uploadFileService.listUploadFiles

		Set<UploadFileListViewVO> allList = uploadFileService.listUploadFilesWithCurrentAccount(criteria);;
		ListResult lr = new ListResult();
		lr.setRows(allList);
		lr.setResults(allList.size());

		return lr;

	}
	
	@RequestMapping(value = "/data/onlineviewer/ieviewer.htm")
	public ModelAndView  onlineOfficeView(String fileUrl , String ext) throws Exception 

{

		ModelAndView mav = new ModelAndView();
		mav.setViewName("/data/onlineviewer/ieviewer"); // view name
		
		return mav;

	}
	
	@RequestMapping(value = "/data/onlineviewer/video/ieviewer.htm")
	public ModelAndView  onlineVideoView(String fileUrl , String ext) throws Exception 

{
		ModelAndView mav = new ModelAndView();
//		UploadFile upladedFile = uploadFileService.loadUploadFile(docId);
//		String fileName = upladedFile.getFileName();
//		File file = uploadFileManager.getRealPath(fileName);
		mav.addObject("fileUrl", "../../file");
		mav.setViewName("/data/onlineviewer/video/ieviewer"); // view name
		return mav;
	}	
	
}
